package com.renwei.yunlong.activity.me;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.PersonChooseBean;
import com.renwei.yunlong.http.Api;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class PersonChooseActivity extends BaseActivity {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.listview)
    ListView listview;
    private PersonAdapter adapter;
    private String personId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_choose);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        personId = getIntent().getStringExtra("personId");

        initView();
        initData();
    }

    private void initView() {
        simpleTileView.setTitle("选择角色");

        adapter = new PersonAdapter(mContext);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PersonChooseBean.RowsBean bean = adapter.getRows().get(position);
                Intent intent = new Intent();
                intent.putExtra("name", bean.getRoleName());
                intent.putExtra("id", bean.getRoleId() + "");
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    private void initData() {
        HttpPresenter hp = new HttpPresenter(this);
        if ("1".equals(companyType)) {
            hp.setUrl(Api.$().OWNER_PERSON_CHOOSE);
        } else if ("2".equals(companyType)) {
            hp.setUrl(Api.$().SERVICE_PERSON_CHOOSE);
        }
        hp.setshowDialogcontent("加载数据");
        hp.setRequsetId(1);
        hp.setCallBack(new HttpTaskListener() {
            @Override
            public void onSuccess(int requestId, String result) {
                PersonChooseBean bean = new Gson().fromJson(result, PersonChooseBean.class);
                if (bean.getMessage().getCode() == 200) {
                    setData(bean.getRows());
                } else {
                    showCenterInfoMsg(bean.getMessage().getMessage());
                }
            }

            @Override
            public void onException(int requestId, String code) {

            }
        });
        hp.post();
    }

    private void setData(List<PersonChooseBean.RowsBean> rows) {
        adapter.setRows(rows);
    }


    public static void OpenActivity(Activity context, int requeCode, String personId){
        Intent intent = new Intent(context, PersonChooseActivity.class);
        intent.putExtra("personId", personId);
        context.startActivityForResult(intent, requeCode);
    }

    class PersonAdapter extends BaseAdapter {

        private Context context;

        public List<PersonChooseBean.RowsBean> getRows() {
            return rows;
        }

        public void setRows(List<PersonChooseBean.RowsBean> rows) {
            this.rows = rows;
            notifyDataSetChanged();
        }

        private List<PersonChooseBean.RowsBean> rows;

        public PersonAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            return rows == null ? 0 : rows.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_function_group_select, parent, false);
            }
            TextView tvName = convertView.findViewById(R.id.tv_function_name);
            ImageView ivCheck = convertView.findViewById(R.id.iv_function_checke);
            tvName.setText(rows.get(position).getRoleName());
            if (!TextUtils.isEmpty(personId)&&personId.equals(String.valueOf(rows.get(position).getRoleId()))){
                ivCheck.setImageResource(R.mipmap.check02);
            } else {
                ivCheck.setImageResource(R.mipmap.check03);
            }

            return convertView;
        }
    }
}
